package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Event4report;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("Event4reportDAO")
@Transactional
public class Event4reportDAOImpl extends AbstractJpaDao<Event4report> implements Event4reportDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Event4report.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public Event4reportDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Event4report findEvent4reportById(Integer id) throws DataAccessException {

		return findEvent4reportById(id, -1, -1);
	}

	@Transactional
	public Event4report findEvent4reportById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEvent4reportById", startResult, maxRows, id);
			return (Event4report) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Event4report> findEvent4reportByName(String name) throws DataAccessException {

		return findEvent4reportByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByName", startResult, maxRows, name);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByNameContaining(String name) throws DataAccessException {

		return findEvent4reportByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByDescription(String description) throws DataAccessException {

		return findEvent4reportByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByDescriptionContaining(String description) throws DataAccessException {

		return findEvent4reportByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByCountry(String country) throws DataAccessException {

		return findEvent4reportByCountry(country, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByCountry(String country, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByCountry", startResult, maxRows, country);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByCountryContaining(String country) throws DataAccessException {

		return findEvent4reportByCountryContaining(country, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByCountryContaining(String country, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByCountryContaining", startResult, maxRows, country);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByProvince(String province) throws DataAccessException {

		return findEvent4reportByProvince(province, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByProvince(String province, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByProvince", startResult, maxRows, province);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByProvinceContaining(String province) throws DataAccessException {

		return findEvent4reportByProvinceContaining(province, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByProvinceContaining(String province, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByProvinceContaining", startResult, maxRows, province);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByCity(String city) throws DataAccessException {

		return findEvent4reportByCity(city, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByCity(String city, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByCity", startResult, maxRows, city);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByCityContaining(String city) throws DataAccessException {

		return findEvent4reportByCityContaining(city, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByCityContaining(String city, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByCityContaining", startResult, maxRows, city);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByAudltertypename(String audltertypename) throws DataAccessException {

		return findEvent4reportByAudltertypename(audltertypename, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByAudltertypename(String audltertypename, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByAudltertypename", startResult, maxRows, audltertypename);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByAudltertypenameContaining(String audltertypename)
			throws DataAccessException {

		return findEvent4reportByAudltertypenameContaining(audltertypename, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByAudltertypenameContaining(String audltertypename, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByAudltertypenameContaining", startResult, maxRows,
				audltertypename);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByFoodtypename(String foodtypename) throws DataAccessException {

		return findEvent4reportByFoodtypename(foodtypename, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByFoodtypename(String foodtypename, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByFoodtypename", startResult, maxRows, foodtypename);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportByFoodtypenameContaining(String foodtypename) throws DataAccessException {

		return findEvent4reportByFoodtypenameContaining(foodtypename, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByFoodtypenameContaining(String foodtypename, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEvent4reportByFoodtypenameContaining", startResult, maxRows, foodtypename);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@Transactional
	public Event4report findEvent4reportByPrimaryKey(Integer id) throws DataAccessException {
		return findEvent4reportByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Event4report findEvent4reportByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEvent4reportByPrimaryKey", startResult, maxRows, id);
			return (Event4report) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Event4report> findEvent4reportsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEvent4reportsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Event4report> findAllEvent4reports() throws DataAccessException {

		return findAllEvent4reports(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findAllEvent4reports(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEvent4reports", startResult, maxRows);
		return new LinkedHashSet<Event4report>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Event4report> findEvent4reportByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Event4report>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Event4report> findEvent4reportsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Event4report.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Event4report> event4reports = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return event4reports;
	}

	@Transactional
	public Event4report findEvent4reportByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findEvent4reportByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Event4report findEvent4reportByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEvent4reportByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Event4report) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Event4report entity) {
		return true;
	}
}
